package com.zx.map.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zx.map.dao.mapper.UserMapper;
import com.zx.map.dao.model.User;
import com.zx.map.dao.model.UserExample;
import com.zx.map.service.UserService;

@Component("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper mapper;

	@Override
	public int delete(String id) {
		mapper.deleteByPrimaryKey(id);
		return 1;
	}

	@Override
	public int add(User user) {
		mapper.insert(user);
		return 1;
	}

	@Override
	public int update(User user) {
		mapper.updateByPrimaryKey(user);
		return 1;
	}

	@Override
	public List<User> query(String shopId,Date begin,Date end) {
		UserExample example = new UserExample();
		UserExample.Criteria criteria = example.createCriteria();
		if(begin!=null && end !=null) {
			criteria.andCreatetimeBetween(begin, end);
		}
		if (StringUtils.isNotBlank(shopId)) {
			criteria.andMerchantidEqualTo(shopId);
		}
		example.setOrderByClause("createtime desc");

		
		return mapper.selectByExample(example);
	}

	@Override
	public long countUsers(String merchantId, Date startDate, Date endDate) {
		UserExample example = new UserExample();
		UserExample.Criteria criteria = example.createCriteria();
		criteria.andMerchantidEqualTo(merchantId);
		if(Objects.nonNull(startDate) && Objects.nonNull(endDate)) {
			criteria.andCreatetimeBetween(startDate, endDate);
		}
		return mapper.countByExample(example);
	}
	@Override
	public long countUsersBefore(String merchantId, Date date) {
		UserExample example = new UserExample();
		UserExample.Criteria criteria = example.createCriteria();
		criteria.andMerchantidEqualTo(merchantId);
		if(Objects.nonNull(date)) {
			criteria.andCreatetimeLessThan(date);
		}
		return mapper.countByExample(example);
	}
}
